/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-05-09
 * Time: 19:57
 */
//10.正则表达式匹配
class Solution5 {
    public boolean isMatch(String s1, String p1) {
        int m = s1.length(), n = p1.length();
        s1 = " " + s1;
        p1 = " " + p1;
        char[] s = s1.toCharArray();
        char[] p = p1.toCharArray();

        boolean[][] dp = new boolean[m+1][n+1];

        dp[0][0] = true;
        for(int j = 2; j <= n; j+=2) {
            if(p[j] == '*') {
                dp[0][j] = true;
            }else {
                break;
            }
        }

        for(int i = 1; i <= m; i++) {
            for(int j = 1; j <= n; j++) {
                if(p[j] == '*') {
                    dp[i][j] = dp[i][j-2] || (s[i] == p[j-1] || p[j-1] == '.' ) && dp[i-1][j];
                }else {
                    dp[i][j] = (p[j] == '.' || s[i] == p[j]) && dp[i-1][j-1];
                }
            }
        }
        return dp[m][n];
    }
}
